<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
          <el-breadcrumb-item>供应商管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-input v-model="dto.name" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" icon="el-icon-search" @click="getSupplierList()">搜索</el-button>
        </el-col>
        <el-col :span="5">
          <el-button type="success" round @click="HandleAdd()">添加供应商</el-button>
        </el-col>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="22">
          <el-table
            :data="supplierList"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left"  class="demo-table-expand">
                  <el-form-item label="联系方式:">
                    <span>{{ props.row.contactphone }}</span>
                  </el-form-item>
                  <el-form-item label="详细地址:">
                    <span>{{ props.row.address }}</span>
                  </el-form-item>
                  <el-form-item label="描述:">
                    <span>{{ props.row.description }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              label="供应商名称"
              width="100">
            </el-table-column>
            <el-table-column
              prop="contactperson"
              label="联系人"
              width="100">
            </el-table-column>
            <el-table-column
              prop="region.name"
              label="所在区域名称"
              width="200">
            </el-table-column>
            <el-table-column
              prop="updatetime"
              label="更新日期"
              width="100">
            </el-table-column>
            <el-table-column
              prop="status"
              label="状态"
              width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
                <el-tag v-else type="danger">冻结</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope"> 
                <el-button
                  size="mini" v-if="scope.row.status == 1" type="warning"
                  @click="handleDisable(scope.row)">冻结</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 0" type="success"
                  @click="handleEnable(scope.row)">解冻</el-button>
                <el-button
                  size="mini" type="primary" icon="el-icon-edit"
                  @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="dto.page"
            :page-size="5"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表单弹出框 -->
    <el-dialog :title="formtitle" :visible.sync="dialogSupplierVisible" width="30%" @close="close('ruleForm')">
      <el-form :model="supplier" :rules="rules" ref="ruleForm">
        <el-form-item label="供应商名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="supplier.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" :label-width="formLabelWidth" prop="contactperson">
          <el-input v-model="supplier.contactperson" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth" prop="contactphone">
          <el-input v-model="supplier.contactphone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所在区域" :label-width="formLabelWidth" prop="areaid" >
          <el-select v-model="supplier.areaid" filterable placeholder="请选择所在区域" style="width: 100%;">
            <el-option
              v-for="item in regionList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="supplier.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            v-model="supplier.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="addSupplier('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    var validateAtLeastOneChange = (rule, value, callback) => {
      if (this.supplier.name == this.oldsupplier.name && this.supplier.areaid == this.oldsupplier.areaid && this.supplier.contactperson == this.oldsupplier.contactperson && this.supplier.contactphone == this.oldsupplier.contactphone && this.supplier.address == this.oldsupplier.address && this.supplier.description == this.oldsupplier.description) {
        callback(new Error('请至少修改一项数据'));
      } else{
        callback();
      }
    }
    return{
      dto:{
        name:"",
        page:1,
        size:5
      },
      total:"",
      supplierList:[],
      regionList:[],
      formtitle:"",
      supplier:{
        id:"",
        name:"",
        contactperson:"",
        contactphone:"",
        address:"",
        description:"",
        areaid:"",
        updatetime:"",
        status:""
      },
      oldsupplier:{
        id:"",
        name:"",
        contactperson:"",
        contactphone:"",
        address:"",
        description:"",
        areaid:"",
        updatetime:"",
        status:""
      },
      dialogSupplierVisible:false,
      formLabelWidth:"100px",
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ],
        contactperson: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ],
        contactphone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ],
        areaid: [
          { required: true, message: '请选择所在地区', trigger: 'change' },
          { validator: validateAtLeastOneChange, trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ]
      }
    }
  },
  mounted(){
    this.getSupplierList()
    this.getRegionList()
  },
  methods:{
    handleCurrentChange(val){
      this.dto.page = val
      this.getSupplierList()
    },
    close(formName){
      this.$refs[formName].clearValidate();
      this.dialogSupplierVisible = false
    },
    HandleAdd(){
      this.supplier = {
        id:"",
        name:"",
      },
      this.dialogSupplierVisible = true
      this.formtitle = "添加供应商"
    },
    handleEdit(row){
      this.dialogSupplierVisible = true
      this.formtitle = "编辑供应商"
      this.oldsupplier = row
      this.supplier.name = row.name
      this.supplier.id = row.id
      this.supplier.contactperson = row.contactperson
      this.supplier.contactphone = row.contactphone
      this.supplier.address = row.address
      this.supplier.description = row.description
      this.supplier.areaid = row.areaid
    },
    addSupplier(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formtitle == '添加供应商') {
            this.$axios.post('/supplier/addSupplier',this.supplier).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '添加成功！',
                  type: 'success'
                });
                this.dialogSupplierVisible = false
                this.getSupplierList()
              }
            })
          }
          if (this.formtitle == '编辑供应商') {
            this.$axios.put('/supplier/editSupplier',this.supplier).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                this.dialogSupplierVisible = false
                this.getSupplierList()
              }
            })
          }     
        } else {
          return false;
        }
      }); 
    },
    getSupplierList(){
      this.$axios.get('/supplier/getSupplierList',{
        params:{
          name: this.dto.name,
          page: this.dto.page,
          size: this.dto.size
        }}).then(res=>{
        if (res.data.code == 200) {
          this.supplierList = res.data.data.data
          this.total = res.data.data.total
        }
      })
    },
    getRegionList(){
      this.$axios.get('/region/getAllRegionList').then(res=>{
        if (res.data.code == 200) {
          this.regionList = res.data.data
        }
      })
    },
    handleEnable(row){
      this.$axios.put('/supplier/enableSupplier/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            message: '解冻成功！',
            type: 'success'
          });
          this.getSupplierList()
        }
      })
    },
    handleDisable(row){
      this.$axios.put('/supplier/disableSupplier/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            message: '冻结成功！',
            type: 'success'
          });
          this.getSupplierList()
        }
        if(res.data.code == 500){
          this.$message.error(res.data.message + '冻结失败！');
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该供应商, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/supplier/deleteSupplier/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.getSupplierList()
        }
        if(res.data.code == 500){
          this.$message.error(res.data.message + '删除失败！');
        }
      })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
}
</script>
<style scoped>
.box-card {
  height: 79vh;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>